package com.rashith.metro.alfa.sam;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.rashith.metro.alfa.R;
import com.rashith.metro.alfa.webservices.StreamHelper;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;





public class ShowListActivity extends Activity {

	private LruCache<String, Bitmap> myCache;

	String id, name;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showlist);

		id = (String) getIntent().getExtras().get("ID");
		name = (String) getIntent().getExtras().get("NAME");
		final int memClass = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE))
				.getMemoryClass();

		final int cacheSize = 1024 * 1024 * memClass / 8;

		myCache = new LruCache<String, Bitmap>(cacheSize);

		ShowList l = new ShowList();
		ListView lv = (ListView) findViewById(R.id.listView1);
		// lv.setAdapter(new
		// ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s));
		TextView tv = (TextView) findViewById(R.id.textView1);
		tv.setText(name);

		MyAdapter adapter = new MyAdapter(this, R.id.textView1,
				R.layout.my_row, l.getPlaces(id));

		lv.setAdapter(adapter);
	}

	private Bitmap getBitmapmFromCache(String key) {
		return myCache.get(key);
	}

	private void addBitmapToCache(String key, Bitmap bitmap) {
		if (getBitmapmFromCache(key) == null) {
			myCache.put(key, bitmap);
		}
	}

	private void loadBitmap(String url, ImageView iv) {
		final Bitmap bitmap = getBitmapmFromCache(url);

		if (bitmap == null) {
			BitmapLoader b = new BitmapLoader(iv);
			b.execute(url);
		} else {
			iv.setImageBitmap(bitmap);
		}
	}

	class MyAdapter extends ArrayAdapter<SamList> {

		List<SamList> list;
		Context ctx;

		public MyAdapter(Context context, int resource, int textViewResourceId,
				List<SamList> objects) {
			super(context, resource, textViewResourceId, objects);
			list = objects;
			ctx = context;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			LayoutInflater inflTER = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View row = inflTER.inflate(R.layout.my_row, parent, false);

			SamList s = list.get(position);

			TextView tv = (TextView) row.findViewById(R.id.textView1);
			tv.setText(s.getItem());

			TextView tv2 = (TextView) row.findViewById(R.id.textView2);
			tv2.setText(s.getItemCategory());

			TextView tv3 = (TextView) row.findViewById(R.id.textView3);
			tv3.setText(s.getDescription());

			TextView tv4 = (TextView) row.findViewById(R.id.textView4);
			tv4.setText(s.getPrice());

			ImageView im = (ImageView) row.findViewById(R.id.imageView1);
			loadBitmap(s.getImageurl(), im);

			if (position % 2 == 1) {
				row.setBackgroundColor(Color.GRAY);
			}
			return row;
		}

	}

	class BitmapLoader extends AsyncTask<String, Integer, Bitmap> {

		String url;
		WeakReference<ImageView> ivRef;

		public BitmapLoader(ImageView imageView) {

			ivRef = new WeakReference<ImageView>(imageView);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			url = params[0];
			if (!url.contains("http"))
				url = "http://" + url;
			Bitmap b = null;
			if (url != null) {
				try {

					InputStream is = (InputStream) new URL(url).getContent();
					b = BitmapFactory.decodeStream(is);

				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			return b;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			if (ivRef != null && bitmap != null) {
				addBitmapToCache(url, bitmap);
				final ImageView iv = ivRef.get();
				if (iv != null)
					iv.setImageBitmap(bitmap);
			}
		}

	}
	
	class WebCaller extends AsyncTask<String, Integer, String>{

	@Override
	protected String doInBackground(String... params) {
		try {
			HttpParams params_ = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params_, 3000);
			HttpConnectionParams.setSoTimeout(params_, 5000);
			
			HttpClient client = new DefaultHttpClient(params_);
			HttpGet get = new HttpGet("http://192.168.1.50:9001/Service.asmx/LocationItems?LID=" + id);
			HttpResponse r = null;
			
			r = client.execute(get);
			
			
			InputStream i =  r.getEntity().getContent(); //(InputStream) new URL("http://192.168.1.50:9001/Service.asmx/HelloWorld").getContent();
			return StreamHelper.convertInputStreamToString(i);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
		
	}
}

